﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

using ERPStore.Extensions;
using ERPStore.Web.Routing;
using ERPStore.Web.Html;

using CloudSoft.Extensions;

namespace ERPStore.Web.Extensions
{
	public static class CheckoutExtensions
	{
		public static void SendPaymentByCardFailed(this Controller controller, ERPStore.Models.OrderCart cart, string message)
		{
			var accountService = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Services.IAccountService>();
			var emailTemplateService = GlobalConfiguration.Configuration.DependencyResolver.GetService<Services.IEmailTemplateService>();
			var emailerService = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Services.EmailerService>();
			var cryptoService = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Services.CryptoService>();

			ERPStore.Models.User user = null;
			string email = null;
			string fullName = null;
			if (cart.CustomerId.GetValueOrDefault(0) != 0)
			{
				user = accountService.GetUserById(cart.CustomerId.Value);

				if (user != null
					&& user.Email.IsNullOrTrimmedEmpty())
				{
					return;
				}
				email = user.Email;
				fullName = user.FullName;
			}
			else
			{
				var registrationUser = accountService.GetRegistrationUser(cart.VisitorId);
				if (registrationUser == null)
				{
					return;
				}
				email = registrationUser.Email;
				fullName = registrationUser.FullName;
			}

			var mailKey = new
			{
				CartCode = cart.Code,
				Email = email,
				FullName = fullName,
				ExpirationDate = DateTime.Now,
				Message = message,
			};

			var encrytpedMailKey = cryptoService.Encrypt(mailKey);
			string encrytpedEmailUrl = controller.Url.RouteERPStoreUrl(ERPStore.Web.Routing.ERPStoreRoutes.EMAILER, new { action = "DirectSendPaymentByCardFailed", key = encrytpedMailKey, });
			encrytpedEmailUrl = encrytpedEmailUrl.FullUrl();

			dynamic emailView = emailTemplateService.CreateEmailView(ERPStore.GlobalConfiguration.Configuration.Settings.Emailer.PaymentFailedView, cart);
			emailView.WebSiteSettings = ERPStore.GlobalConfiguration.Configuration.Settings;
			emailView.EncryptedUrl = encrytpedEmailUrl;
			emailView.FullName = fullName;
			emailView.Message = message;
			emailView.Email = email;

			var emailMessage = emailTemplateService.CreateEmailMessage(emailView);

			emailerService.Send(emailMessage);
		}

		public static void SendCustomerOrderConfirmation(this Controller controller, ERPStore.Models.ISaleDocument order, string password)
		{
			if (order.User.Email.IsNullOrTrimmedEmpty())
			{
				return;
			}

			var emailTemplateService = GlobalConfiguration.Configuration.DependencyResolver.GetService<Services.IEmailTemplateService>();
			var emailerService = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Services.EmailerService>();
			var cryptoService = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Services.CryptoService>();

			var mailKey = new
			{
				Code = order.Code,
				Type = (order is ERPStore.Models.Order) ? "order" : "quote",
				Salt = DateTime.Now,
			};

			var encrytpedMailKey = cryptoService.Encrypt(mailKey);
			string encrytpedEmailUrl = string.Format("http://{0}{1}", controller.HttpContext.Request.Url.Host, controller.Url.RouteERPStoreUrl(ERPStoreRoutes.EMAILER, new { action = "DirectOrderConfirmation", key = encrytpedMailKey, }));

			dynamic emailView = emailTemplateService.CreateEmailView(ERPStore.GlobalConfiguration.Configuration.Settings.Emailer.OrderConfirmationView, order);
			emailView.WebSiteSettings = ERPStore.GlobalConfiguration.Configuration.Settings;
			emailView.EncryptedUrl = encrytpedEmailUrl;
			emailView.FullName = order.User.FullName;
			emailView.Password = password;

			var emailMessage = emailTemplateService.CreateEmailMessage(emailView);
			emailerService.Send(emailMessage);
		}

		public static void SendAdminCustomerOrderConfirmation(this Controller controller, ERPStore.Models.ISaleDocument order, string cartId, string ip)
		{
			if (order.User.Email.IsNullOrTrimmedEmpty())
			{
				return;
			}

			var emailTemplateService = GlobalConfiguration.Configuration.DependencyResolver.GetService<Services.IEmailTemplateService>();
			var emailerService = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Services.EmailerService>();
			var cryptoService = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Services.CryptoService>();
			var cartService = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Services.ICartService>();

			var type = (order is ERPStore.Models.Order) ? "order" : "quote";

			var mailKey = new
			{
				Code = order.Code,
				Type = type,
				Salt = DateTime.Now,
			};

			var encryptedTicket = cryptoService.EncryptOrderConfirmation(order.Code, DateTime.Now.AddDays(10), false);
			string encryptedUrl = string.Format("http://{0}{1}{2}", controller.Request.Url.Host, controller.Url.RouteERPStoreUrl(ERPStoreRoutes.ORDER_DETAIL), encryptedTicket);

			var cart = cartService.GetCartByCode(cartId);

			dynamic emailView = emailTemplateService.CreateEmailView(ERPStore.GlobalConfiguration.Configuration.Settings.Emailer.AdminOrderConfirmationView, order);
			emailView.WebSiteSettings = ERPStore.GlobalConfiguration.Configuration.Settings;
			emailView.FullName = order.User.FullName;
			emailView.OrderCart = cart;
			emailView.IP = ip;
			emailView.EncryptedUrl = encryptedUrl;

			var emailMessage = emailTemplateService.CreateEmailMessage(emailView);

			emailerService.Send(emailMessage);
		}


	}
}
